home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / byte1286.arc / PERRY.ARC / DEMO-ONE.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1980-01-01  |  7.0 KB  |  345 lines

  1. program DemoOne;
  2. {one dimensional cellular automata }
  3. var
  4.   FilVar:    text;
  5.   Line:      string[20];
  6.   C:         string[1];
  7.  
  8. Ainit: array[0..4000] of byte; 
  9.       {4001 cells wide. Allows for}
  10. Afield: array[0..4000] of byte; 
  11.       { expansion of COMPUTE FIELD }
  12.   Bfield:    array[0..4000] of byte;
  13.   Rule:        array[0..12] of byte;
  14.   I,J,M,N,H,P,V,X,Nix:  integer;
  15.   Ch:               char;
  16.  
  17.  Delta:    integer; { pixel spacing  1, or 2 }
  18.  Dwidth:   Integer; { width of display field }
  19.  Cwidth:   Integer; { width of compute field }
  20.  
  21.  Cstart:   integer; { COMPUTE FIELD. start with a }
  22.  Cfinish:  integer; { width of 160 }
  23.  
  24.  Dstart:   integer; { display field }
  25.  Dfinish:  integer;
  26.  
  27.  Vstart:   integer; { vertical display }
  28.  Vfinish:  integer;
  29.  
  30.  Hstart:   integer; { horizontal display }
  31.  Hfinish:  integer;
  32.  
  33. const  { typed constants }
  34.        { these are essentially initialized variables }
  35.  
  36.   Widen:    Integer = 0;
  37.   Bgnd:     Integer = 0;
  38.  
  39.   k:        integer = 4;   { number of states }
  40.   RuleEnd:  integer = 9;   {  RuleEnd = 3 * (k - 1) }
  41.   r:        integer = 1;   { Range; number of neighbors }
  42.  
  43.  
  44.  
  45. const
  46.   Center = 2000;    {center of fields}
  47. { **********  start of procedures  **************** }
  48.  
  49. {------------------ 1 ------------------}
  50.  
  51. procedure DisplayMessage;
  52. begin
  53.   GoToXY(1,25);
  54.   Write('DEMO-ONE: by Kenneth E. Perry. 
  55.   Press Ins');
  56. end;
  57.  
  58. procedure DisplayStatusLine;
  59. begin
  60.   GoToXY(1,25);
  61.   Write('                                     ');
  62.   GoToXY(1,25);
  63.   Write(Rule[0]);
  64.   for I := 1 to 3 do
  65.   begin
  66.     write(' ');
  67.     for J := 1 to 3 do
  68.     begin
  69.       write(Rule[3*(I-1)+J]);
  70.     end;
  71.   end;
  72. {  Write('    ');
  73.   Write(Bgnd);
  74.   Write('    ');
  75.   Write(Cwidth);   }
  76. end;            {DisplayStatusLine}
  77.  
  78. {------------------ 2 ------------------}
  79.  
  80. procedure DisplayGenerations;
  81. { compute and display 190 generations 
  82. ( or rows of cells ) }
  83. begin
  84.  for V := Vstart to Vfinish do    
  85. { number of generations to display }
  86.  begin
  87.              { show display field }
  88.       if Delta = 1 then
  89.       begin
  90.         for H := Hstart to Hfinish do
  91.         begin
  92.           I := H + Dstart;  { display one generation }
  93.           plot(H,V,Afield[I]);
  94.         end;
  95.       end;
  96.  
  97.       if Delta = 2 then
  98.       begin
  99.         for H := Hstart to Hfinish do
  100.         begin
  101.           I := H + Dstart;
  102.           plot(H+H,V+V,Afield[I]);
  103.         end;
  104.       end;
  105.  
  106.    { check for overflow of COMPUTE FIELD }
  107.  
  108.       if Widen = 1 then
  109.       begin
  110.         I := Cstart;
  111.         J := Cfinish;
  112.         if (Afield[I] <> Afield[I + 1]) or 
  113.            (Afield[J - 1] <> Afield[J]) then
  114.         begin
  115.           Cstart := Cstart - 1;   
  116.                     { this is to avoid end effects }
  117.           Cfinish := Cfinish + 1;
  118.           Cwidth := Cfinish - Cstart;
  119.         end;
  120.       end;
  121.  
  122.  {compute new row of cells and place in Bfield }
  123.  
  124.       for I := Cstart to Cfinish do
  125.       begin
  126.         N := Afield[I-1] + Afield[I] + Afield[I+1];
  127.         Bfield[I] := Rule[N];
  128.       end;
  129.  
  130.                {return Bfield to Afield}
  131.       for I := Cstart to Cfinish do
  132.       begin
  133.         Afield[I] := Bfield[I];
  134.       end;
  135.  
  136.     end; {for}
  137. end;  { DisplayGenerations }
  138.  
  139. {------------------ 3 ------------------}
  140.  
  141.   procedure ReadRuleFromFile;
  142.   begin {read rule from file 'DEMO-C.DOC' into 'Line'}
  143.     Readln(FilVar,Line);
  144.  
  145.     J := 0;
  146.     for I := 1 to 13 do
  147.     begin
  148.       C := Copy(Line,I,1);
  149.       { copy rule, one digit at a time }
  150.       if (C <> ' ') then     { skipping spaces }
  151.       begin
  152.         Val(C,M,Nix);   
  153.         { value of String[1] C placed in integer M }
  154.         Rule[J] := M;   
  155.         { copy rule from 'Line' into 'Rule' }
  156.         J := J + 1;
  157.       end;
  158.     end;
  159.   end;      { ReadRuleFromFile }
  160.  
  161. {------------------ 6 ------------------}
  162.  
  163. procedure InitializeAinitToBackground;
  164. begin
  165.   for I := 0 to 4000 do
  166.   begin
  167.     Ainit[I] := Bgnd;
  168.   end;
  169. end;
  170.  
  171.  
  172. {------------------ 7 ------------------}
  173.  
  174. procedure InitializeAinitRandom;
  175. begin
  176. { random initialize of COMPUTE FIELD in Ainit}
  177.     for I := Cstart to Cfinish do
  178.     begin
  179.       Ainit[I] := Random(k);
  180.     end;
  181. end;      { InitializeAinitRandom }
  182.  
  183. {------------------ 8 ------------------}
  184.  
  185. procedure MoveAinitToAfield;
  186. begin
  187.     for I := 0 to 4000 do
  188.     begin
  189.       Afield[I] := Ainit[I];
  190.     end;
  191. end;
  192.  
  193.  
  194. {------------------ 11 -----------------}
  195.  
  196. procedure StartFinish;
  197. begin
  198.   Cstart := Center - (Cwidth div 2);
  199.   Cfinish := Center + (Cwidth div 2) - 1;
  200.   Dstart := Center - (Dwidth div 2);
  201.   Dfinish := Center + (Dwidth div 2) - 1;
  202. end;
  203.  
  204.  
  205. {------------------ 13 -----------------}
  206.  
  207. procedure Field160X95;
  208. begin
  209.   GraphColorMode;
  210.   Dwidth := 160;
  211.   Cwidth := 160;
  212.  
  213.   StartFinish;
  214.  
  215.   Vstart := 0;
  216.   Vfinish := 94;
  217.   Hstart := 0;
  218.   Hfinish := 159;
  219.   Delta := 2;
  220.   Delay(400);
  221.   DisplayStatusLine;
  222. end;
  223.  
  224. {------------------ 14 -----------------}
  225.  
  226. procedure Field320X190;
  227. begin
  228.   GraphColorMode;
  229.   Dwidth := 320;
  230.   Cwidth := 320;
  231.  
  232.   StartFinish;
  233.  
  234.   Vstart := 0;
  235.   Vfinish := 189;
  236.   Hstart := 0;
  237.   Hfinish := 319;
  238.   Delta := 1;
  239.   Delay(400);
  240. end;
  241.  
  242.  
  243.  
  244.  
  245. { **************   end  of procedures **************** }
  246.  
  247.  
  248.  
  249. { ************** MAIN PROGRAM ********************** }
  250.  
  251.  
  252. begin
  253.  
  254. Ch := ' ';
  255. GraphColorMode;
  256. Palette(0);
  257. Randomize;
  258. Field320X190;
  259. DisplayMessage;
  260.  
  261.  
  262. Assign(FilVar,'DEMO-1.DOC');
  263. Reset(FilVar);
  264.  
  265. repeat
  266.   if KeyPressed then
  267.   begin                        {keypad symbols}
  268.     Read(Kbd,Ch);
  269.     if (Ch = #45) then               { - }
  270.     begin
  271.     end;
  272.  
  273.  
  274.     if (Ch = #43) then               { + }
  275.     begin                    { Continue Structure }
  276.       DisplayStatusLine;
  277.       DisplayGenerations;
  278.     end;
  279.  
  280.         {escape sequences}
  281.  
  282.     if (Ch = #27) and KeyPressed then {one more char?}
  283.     begin
  284.       Read(Kbd,Ch);
  285.  
  286.       if (Ch = #82) then               { ins }
  287.       begin               { new rule random initialize }
  288.        Widen := 0;
  289.        ReadRuleFromFile;
  290.        DisplayStatusLine;
  291.        InitializeAinitToBackground;
  292.        InitializeAinitRandom;
  293.        MoveAinitToAfield;
  294.        DisplayGenerations;
  295.       end;
  296.  
  297.       if (Ch = #83) then      { del }
  298.       begin              { Same Rule Random Inititialize }
  299.         Widen := 0;
  300.         DisplayStatusLine;
  301.         InitializeAinitRandom;
  302.         MoveAinitToAfield;
  303.         DisplayGenerations;
  304.       end;
  305.  
  306.  
  307.         {function keys}
  308.  
  309.       if (Ch = #59) then           { F1 }
  310.       begin
  311.       end;
  312.  
  313.       if (Ch = #60) then           { F2 }
  314.       begin
  315.       end;
  316.  
  317.       if (Ch = #61) then           { F3 }
  318.       begin
  319.       end;
  320.  
  321.  
  322.       if (Ch = #66) then           { F8 }
  323.       begin
  324.       end;
  325.  
  326.       if (Ch = #67) then           { F9 }
  327.       begin
  328.       field160X95;
  329.       end;
  330.  
  331.       if (Ch = #68) then           { F10 }
  332.       begin
  333.       Field320X190;
  334.       end;
  335.  
  336.     end;  { if (Ch = #27 }
  337.   end;  { if keypressed }
  338. until Ch = #13;  { Return }  { end repeat }
  339.  
  340.  
  341. end.
  342.  
  343.  
  344.  
  345.